//
//  UserInfo.m
//  夺宝吧
//
//  Created by xiaocai on 15/6/19.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "UserInfo.h"

@implementation UserInfo
//获取用户实例
- (instancetype)init
{
    if (self = [super init]) {
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        self.user_token = [user objectForKey:@"user_token"];
        self.user_token_outTime = [user objectForKey:@"user_token_outTime"];
        self.user_token_refresh = [user objectForKey:@"user_token_refresh"];
        self.user_uid = [user objectForKey:@"user_uid"];
        self.user_name = [user objectForKey:@"user_name"];
        self.user_email = [user objectForKey:@"user_email"];
        self.user_phone = [user objectForKey:@"user_phone"];
        self.user_money = [user objectForKey:@"user_money"];
        self.user_jifen = [user objectForKey:@"user_score"];
        self.user_header_img_path = [user objectForKey:@"user_header_img_path"];
        self.user_age = [user objectForKey:@"user_age"];
        self.user_sex = [user objectForKey:@"user_sex"];
    }
    return self;
}
//单例
//+ (instancetype)shareUserInfo{
//    static UserInfo *user = nil;
//    static dispatch_once_t once;
//    dispatch_once(&once, ^{
//        user = [[UserInfo alloc]init];
//        
//    });
//    return user;
//}
//判断用户是否处于登录状态
+ (BOOL)isUserLogin
{
    UserInfo *user = [[UserInfo alloc]init];
    if (user.user_token != nil) {
        return YES;
    }
    return NO;
}
//判断用户是否保存过用户信息
+ (BOOL)isUserInfoExist
{
    UserInfo *user = [[UserInfo alloc]init];
    if (user.user_phone != nil || user.user_email != nil) {
        return YES;
    }
    return NO;
}
//保存用户信息
+ (void)writeUserInfoWithDic:(NSDictionary *)dic
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    // 认证token
    if ([dic[@"token"] class]!= [NSNull class]) {
        [user setObject:dic[@"token"]forKey:@"user_token"];
    }
    // token过期时间
    if ([dic[@"expire"] class]!= [NSNull class]) {
       [user setObject:dic[@"expire"]forKey:@"user_token_outTime"];
    }
    // 允许刷新认证token
    if ([dic[@"refresh_token"] class]!= [NSNull class]) {
        [user setObject:dic[@"refresh_token"]forKey:@"user_token_refresh"];
    }
    // 用户uid
    if ([dic[@"uid"] class]!= [NSNull class]) {
        [user setObject:dic[@"uid"]forKey:@"user_uid"];
    }
    // 用户名
    if ([dic[@"username"] class]!= [NSNull class]) {
        [user setObject:dic[@"username"]forKey:@"user_name"];
    }
    if ([dic[@"email"] class]!= [NSNull class]) {
        // 用户邮箱
        [user setObject:dic[@"email"]forKey:@"user_email"];
    }
    // 用户手机
    if ([dic[@"mobile"] class]!= [NSNull class]) {
        [user setObject:dic[@"mobile"]forKey:@"user_phone"];
    }
    //头像
    if ([dic[@"img_url"] class]!= [NSNull class]) {
        [user setObject:dic[@"img_url"]forKey:@"img_url"];
    }
    //夺币
    if ([dic[@"money"] class]!= [NSNull class]) {
        [user setObject:dic[@"money"]forKey:@"user_money"];
    }
    //积分
    if ([dic[@"score"] class]!= [NSNull class]) {
        [user setObject:dic[@"score"]forKey:@"user_score"];
    }
    //是否上架标志show_pay
    if ([dic[@"show_pay"] class]!= [NSNull class]) {
        NSNumber *flag = dic[@"show_pay"];
        NSLog(@"1--->%d",[flag intValue]);
        [user setObject:flag forKey:@"user_show_pay"];
    }
    //保存用户信息
    [user synchronize];
    
}
//刷新用户信息
+ (void)updateUserInfoWithDic:(NSDictionary *)dic
{
    if ([dic class] != [NSNull class] && dic != nil) {
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        // 用户uid
        if ([dic[@"uid"] class]!= [NSNull class]) {
            [user setObject:dic[@"uid"]forKey:@"user_uid"];
        }
        // 用户名
        if ([dic[@"username"] class]!= [NSNull class]) {
            [user setObject:dic[@"username"]forKey:@"user_name"];
        }
        //头像
        if ([dic[@"img_url"] class]!= [NSNull class] ) {
            [user setObject:dic[@"img_url"]forKey:@"user_header_img_path"];
        }
        
        // 用户手机
        if ([dic[@"mobile"] class]!=[NSNull class]) {
            [user setObject:dic[@"mobile"] forKey:@"user_phone"];
        }
        //夺币
        if ([dic[@"money"] class]!= [NSNull class]) {
            [user setObject:dic[@"money"]forKey:@"user_money"];
        }
        //积分
        if ([dic[@"score"] class]!= [NSNull class]) {
            [user setObject:dic[@"score"]forKey:@"user_score"];
        }
        //性别
        if ([dic[@"gender"] class]!= [NSNull class]) {
            NSString *flag = dic[@"gender"];
            NSString *sex = @"男";
            if ([flag isEqualToString:@"-1"]) {
                sex = @"保密";
            }else if([flag isEqualToString:@"0"]){
                sex = @"女";
            }
            [user setObject:sex forKey:@"user_sex"];
        }
        //年龄
        if ([dic[@"age"] class]!= [NSNull class]) {
            [user setObject:dic[@"age"]forKey:@"user_age"];
        }
        
        //保存用户信息
        [user synchronize];

    }
}
//删除用户信息
+ (void)deleteUserInfo
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    /*************
     *为提高用户体验，用户的账号不予删除 方便用户下次登录
     *****************/
    [user removeObjectForKey:@"user_token"];
    [user removeObjectForKey:@"user_token_outTime"];
    [user removeObjectForKey:@"user_token_refresh"];
    [user removeObjectForKey:@"user_uid"];
    [user removeObjectForKey:@"user_name"];
}
//快捷获取用户id
+ (NSString *)getUserID
{
    return [[UserInfo alloc]init].user_uid;
}
//快捷获取用户token
+ (NSString *)getUserToken
{
    return [[UserInfo alloc]init].user_token;
}
//快捷获取用户refreshtoken
+ (NSString *)getRefreshToken
{
    return [[UserInfo alloc]init].user_token_outTime;
}
//快捷获取用户账号
+ (NSString *)getUserCount
{
    UserInfo *user = [[UserInfo alloc]init];
    if (user.user_phone != nil) {
        return user.user_phone;
    }
    return user.user_email;
}



/************分享部分****************/
//设置分享内容
+ (void)setShareTitle:(NSString *)title Content:(NSString *)content ShareURL:(NSString *)shareUrl
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:[WYPublic getUsefullStr:title] forKey:@"user_share_title"];
    [user setObject:[WYPublic getUsefullStr:content] forKey:@"user_share_content"];
    [user setObject:[WYPublic getUsefullStr:shareUrl] forKey:@"user_share_shareUrl"];
    [user synchronize];
}
//获取分享标题
+(NSString *)getShareTitle
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    return [user objectForKey:@"user_share_title"];
}
//获取分享文本
+(NSString *)getShareContent
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    return [user objectForKey:@"user_share_content"];
}
//获取分享链接
+(NSString *)getShareUrl
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    return [user objectForKey:@"user_share_shareUrl"];
}

//一键加群的key
+(void)keyForAddQQ:(NSString *)key Num:(NSString *)qq
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:key forKey:@"QQKEY"];
//    [user setObject:qq forKey:@"QQNUM"];
    [user synchronize];
}
+(NSString*)getKeyForQQ
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    return [user objectForKey:@"QQKEY"];
}
+(NSString*)getNumForQQ
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    return [user objectForKey:@"QQNUM"];
}
@end
